#include "drv_sht20.h"
#include "drv_iic.h"
#include "drv_systick.h"
#include "stdio.h"
#define ACK 0
#define NOACK 1
sht20_val tem_hum_data;
void drv_sht20_init(void)
{
	drv_iic_init();
}

uint16_t drv_sht20_read(void)
{
	uint8_t data[2];
	uint16_t ret=0;
	drv_iic_start();
	drv_iic_senddata(0x80);
	if(drv_iic_waitACK()!=ACK)
	   {
		drv_iic_stop();
		return NOACK;
		}
	
	drv_iic_start();
	drv_iic_senddata(0xe3);
	if(drv_iic_waitACK()!=ACK)
	{
		drv_iic_stop();
		return NOACK;
	}
	drv_iic_start();
	drv_iic_senddata(0x81);
	if(drv_iic_waitACK() !=ACK)
	{
		drv_iic_stop();
		return NOACK;
	}
	drv_systick_ms(85);
	data[0]=drv_iic_readdata(ACK);
	data[1]=drv_iic_readdata(NOACK);
	drv_iic_stop();
	ret = ((data[0]<<8)|data[1]);
	printf("data[0]=%d data[1]=%d\r\n",data[0],data[1]);
	return ret;
}

uint16_t drv_sht20_rest(void)
{
		drv_iic_start();
		drv_iic_senddata(0x80);
		if(drv_iic_waitACK()!=ACK)
		   {
			drv_iic_stop();
			return NOACK;
			}
		drv_iic_senddata(0xfe);
		if(drv_iic_waitACK()!=ACK)
		{
			drv_iic_stop();
			return NOACK;
		}
		drv_iic_stop();
}


void drv_sht20_getdata(void)
{	uint16_t temp=0;
	temp = drv_sht20_read();
	tem_hum_data.tem = -46.85+72*((float)temp/65536);
	printf("Temperature=%.2f\r\n",tem_hum_data.tem);
}


